import React, { Component } from 'react'
 import Titlehed from './Titlehed'
 import Titlemain from './Titlemain'
 import axios from "axios"
export class Main extends Component {
    constructor(props) {
      super(props)
    
      this.state = {
         list:[],
         newArr:[]
      }
    }
    componentDidMount() { 
        axios.get("/list").then(res=>{
            this.setState({
                list:res.data,
                newArr:res.data,
                active:0
            })
        })
     }
     tab=(index)=>{
         this.setState({
             active:index
         })

     }
     del=(id)=>{
         let {list}=this.state
       list=list.filter((item,index)=>{
             return item.id!==id
         })
        this.setState({
          list
        })
    }
     add=()=>{
        let {list}=this.state
       list.push({
           id:new Date()*1,
           "title":"tab新内容"
       })
       this.setState({
           list
       })

     }
  render() {
      const {list,active}=this.state
    return (
      <div>
          <Titlehed list={list} tab={this.tab} del={this.del} add={this.add} active={active}></Titlehed>
          <Titlemain cont={list[active] ? list[active].title :''}></Titlemain>
      </div>
    )
  }
}

export default Main